#	$OpenBSD: Makefile.bsd-wrapper,v 1.63 2012/12/09 22:04:51 miod Exp $

MAN=		gcc.1 cpp.1 gcov.1 protoize.1
MLINKS+=	protoize.1 unprotoize.1
MLINKS+=	gcc.1 cc.1

BINDIR=/usr/bin
.if defined(BOOTSTRAP)
LANGUAGES=--enable-languages=c
.else
LANGUAGES=--enable-languages='c,c++,objc' 
MLINKS+=	gcc.1 g++.1 gcc.1 c++.1
.endif
V=3.3.6

.SUFFIXES:	.1
.PATH.1:	${.CURDIR}/gcc ${.CURDIR}/gcc/doc

LIBIBERTY_INCLUDES=${.CURDIR}/../../lib/libiberty/include
GNUCFLAGS=	${CFLAGS} ${COPTS} -DOPENBSD_NATIVE \
		-I${LIBIBERTY_INCLUDES}

MAKE_FLAGS= 	CFLAGS="${GNUCFLAGS}" \
		CC="${CC}" LDFLAGS="${LDSTATIC}" \
		DEMANGLER_PROG= \
		DEMANGLE_H= \
		LIBIBERTY_INCLUDES=${LIBIBERTY_INCLUDES} \
		LIBGCC2_INCLUDES=-I${LIBIBERTY_INCLUDES} \
		BUILD_LIBIBERTY=-liberty \
		LIBIBERTY=-liberty \
		HOST_LIBDEPS= \
		LIBDEPS= \
		build_infodir=.

GXX_INCDIR=	/usr/include/g++
CLEANFILES=	.gdbinit */.gdbinit *.info* \
		c-parse.c c-parse.h c-parse.y cexp.c cp/parse.c cp/parse.h \
		f/*.info* objc/objc-parse.c objc/objc-parse.y target_path \
		print-rtl1.c gengtype-lex.c gengtype-yacc.c gengtype-yacc.h \
		genrtl.c genrtl.h fixinc.sh fixinc/Makefile \
		po/Makefile  po/Makefile.in  po/POTFILES

GCCARCH=	${MACHINE_ARCH}-unknown-openbsd${OSREV}
GCCINC=	/usr/lib/gcc-lib/${GCCARCH}/$V/include

# cross-compilation
.if defined(CROSS_HOST) && defined(TARGET_ARCH) && defined(CROSSDIR)
CONFIGTARGET=	--build=${CROSS_HOST}-unknown-openbsd${OSREV} \
    --target=${TARGET_ARCH}-unknown-openbsd${OSREV}

CONFIGPREFIX=	${CROSSDIR}/usr
PATH=		/bin:/usr/bin:/sbin:/usr/sbin:${CROSSDIR}/usr/bin
GXX_INCDIR=	${CROSSDIR}/usr/include/g++
GNUCFLAGS=	${CFLAGS} ${COPTS} -DOPENBSD_CROSS -I${LIBIBERTY_INCLUDES}
HOSTCFLAGS=	${CFLAGS} ${COPTS} -DOPENBSD_CROSS -I${LIBIBERTY_INCLUDES}
HOST_CC=cc
MAKE_FLAGS+= 	CFLAGS_FOR_BUILD="${HOSTCFLAGS}" HOST_CC=cc
.elif defined(TARGET_ARCH) && defined(CROSSDIR)
CONFIGTARGET=	--target=${TARGET_ARCH}-unknown-openbsd${OSREV}
CONFIGPREFIX=	${CROSSDIR}/usr
MAKE_FLAGS+=	GCC_FOR_TARGET="./xgcc -B./ -I${CROSSDIR}/usr/include"
PATH=		/bin:/usr/bin:/sbin:/usr/sbin:${CROSSDIR}/usr/bin
GXX_INCDIR=	${CROSSDIR}/usr/include/g++
GNUCFLAGS=	${CFLAGS} ${COPTS} -DOPENBSD_CROSS -I${LIBIBERTY_INCLUDES}
.else
CONFIGTARGET=
CONFIGPREFIX=	/usr
PATH=		/bin:/usr/bin:/sbin:/usr/sbin
.endif

all:	config.status
	exec env PATH="${PATH}" ${MAKE} ${MAKE_FLAGS} all doc proto

tconfig.h: config.status
	exec ${MAKE} ${MAKE_FLAGS} tconfig.h

tree-check.h: config.status
	exec ${MAKE} ${MAKE_FLAGS} $@


.FORCE:	.IGNORE

.include <bsd.own.mk>

.ifdef GLOBAL_AUTOCONF_CACHE
CF=	--cache-file=${GLOBAL_AUTOCONF_CACHE}
.else
CF=
.endif

config.status: gcc/Makefile.in gcc/configure gcc/c-parse.in
	PATH="${PATH}" \
	INSTALL_PROGRAM="${INSTALL} ${INSTALL_COPY} ${INSTALL_STRIP}" \
	CPPFLAGS=-I${LIBIBERTY_INCLUDES} \
	MAKEINFO='makeinfo --no-split' \
	/bin/sh ${.CURDIR}/gcc/configure --with-gnu-as --with-gnu-ld \
		--prefix=${CONFIGPREFIX} --mandir='$$(prefix)/share/man' \
		--with-local-prefix=/usr ${CF} \
		${LANGUAGES} --enable-cpp --disable-nls \
		--with-gxx-include-dir=${GXX_INCDIR} \
		--enable-__cxa_atexit \
		--enable-threads=posix \
		${CONFIGTARGET} ${GCCARCH} && touch config.status

.ifdef NOMAN
maninstall:
	@echo NOMAN is set
.endif

# XXX INSTALL_TARGET should be install-normal, more or less
# the only reason is not so is because our man handling is special
# (pre-formatted) and also because of cpp, that we install by hand
# elsewhere, until we get rid of that old `compatibility' script.

target_path: config.status
	__target=`sed -n '/^target=/s/target=//p' Makefile`; \
	__version=`sed -n '/^gcc_version=/s/gcc_version=//p' Makefile`; \
	echo /usr/lib/gcc-lib/$$__target/$$__version >target_path

install:	maninstall target_path
	${MAKE} ${MAKE_FLAGS} \
		prefix=/usr \
		INSTALL_PROGRAM="${INSTALL} ${INSTALL_COPY} ${INSTALL_STRIP}" \
		GXX_TARGET_INSTALL_NAME=${.OBJDIR}/dummy1 \
		CXX_TARGET_INSTALL_NAME=${.OBJDIR}/dummy2 \
		infodir=/usr/share/info \
		tooldir=/tmp assertdir=/tmp \
		bindir=$(BINDIR) \
		install-common install-multilib \
		install-info intl.install \
		lang.install-normal
	chown -R ${BINOWN}:${BINGRP} ${DESTDIR}/usr/lib/gcc-lib
	chmod -R a+rX ${DESTDIR}/usr/lib/gcc-lib
	mkdir -p ${DESTDIR}/usr/libexec ${DESTDIR}/usr/bin
#	if [ -e ${DESTDIR}/usr/lib/libf2c.a ]; then \
#		chown ${BINOWN}:${BINGRP} ${DESTDIR}/usr/lib/libf2c.a; \
#		chmod 444 ${DESTDIR}/usr/lib/libf2c.a; \
#	fi
	if [ -e ${DESTDIR}/usr/include/f2c.h ]; then \
		chown ${BINOWN}:${BINGRP} ${DESTDIR}/usr/include/f2c.h; \
		chmod 444 ${DESTDIR}/usr/include/f2c.h; \
	fi
	${INSTALL} ${INSTALL_COPY} ${INSTALL_STRIP} -m 755 cpp \
	   ${DESTDIR}/usr/libexec/cpp
	${INSTALL} ${INSTALL_COPY} ${INSTALL_STRIP} -m 755 xgcc \
	   ${DESTDIR}/usr/bin/gcc
	ln -f ${DESTDIR}/usr/bin/gcc ${DESTDIR}/usr/bin/cc

clean cleandir: 
	-@if [ -e Makefile ]; then ${MAKE} distclean; fi
	-rm -f ${CLEANFILES}

depend: config.status
	exec ${MAKE} ${MAKE_FLAGS} generated-manpages

FILES= ${.CURDIR}/gcc/unwind.h
.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "amd64"
FILES+=${.CURDIR}/gcc/config/i386/mmintrin.h \
	${.CURDIR}/gcc/config/i386/xmmintrin.h \
	${.CURDIR}/gcc/config/i386/emmintrin.h \
	${.CURDIR}/gcc/config/i386/pmmintrin.h
.elif ${MACHINE_ARCH} == "powerpc"
FILES+=${.CURDIR}/gcc/config/rs6000/altivec.h
.endif

includes:
	mkdir -p ${DESTDIR}${GCCINC}
	for i in ${FILES}; do \
		j=`basename $$i`; \
		cmp -s $$i ${DESTDIR}${GCCINC}/$$j || \
		    ${INSTALL} ${INSTALL_COPY} -m 444 $$i ${DESTDIR}${GCCINC}/$$j; \
		ln -sf ${GCCINC}/$$j ${DESTDIR}/usr/include/$$j; \
	done

lint:
	# Nothing here so far...

tags:
	# Nothing here so far...

.include <bsd.obj.mk>
.include <bsd.subdir.mk>
.ifndef NOMAN
.include <bsd.man.mk>
.endif
